const express = require('express');
const request = require('request');
const session = require('express-session')
const router = express.Router();
/* GET query page. */
router.get('/', function(req, res, next) {
    if(req.session.logged){
        const url = 'http://dormlist:5000/query'
        request({
            url: url,
            method: "GET",
        }, function(error, response, body){
            if(!error && response.statusCode == 200){

                body = JSON.parse(body)
                if(body.status == 1){
                    dormlist = JSON.parse(body.dormlist)
                    res.render('query', dormlist)
                }
                else{
                    res.send(body)
                }
                
            }
            else
            {
                res.send('Error, try again')
            }

        });


    }
    else {
        res.redirect('/signin')
    }
    // if(req.session.userName){
    //     res.render('query');

    // }
    // else{
    //     res.send(req.session)
    // }
  
});

module.exports = router;